package com.jwb.config.security;

import com.alibaba.fastjson.JSONObject;
import com.jwb.pub.service.MenuService;
import com.jwb.pub.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Slf4j
@Component
public class SuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MenuService menuService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String name = request.getParameter("username");
        JSONObject jsdata = new JSONObject();
        jsdata.put("name", name);
        JSONObject jso =userInfoService.getUser(jsdata);
        JSONObject info = jso.getJSONArray("list").getJSONObject(0);
        HttpSession session = request.getSession();
        session.setAttribute("userName", name);
        session.setAttribute("userId", info.getString("user_id"));
        session.setAttribute("curDeptId", info.getString("cur_dept_id"));
        session.setAttribute("curDeptName", info.getString("cur_dept_name"));
        session.setAttribute("curDeptLevel", info.getString("cur_dept_level"));
        session.setAttribute("roleList", info.getString("role_list"));
        session.setAttribute("realName", info.getString("real_name"));
        session.setAttribute("contactPhone", info.getString("contact_phone"));
        session.setAttribute("operDeptId", info.getString("oper_dept_id"));
        session.setAttribute("operDeptName", info.getString("oper_dept_name"));
        session.setAttribute("operDeptLevel", info.getString("oper_dept_level"));
        session.setAttribute("privNames", info.getString(""));
        session.setAttribute("rank", info.getString("rank"));
        session.setAttribute("deptCode", info.getString("dept_code"));
        session.setAttribute("groupCode", info.getString("group_code"));
        session.setAttribute("rankDeptId", info.getString("rank_dept_id"));
        session.setAttribute("kpperson", info.getString("kp_person"));
        session.setAttribute("psrange", info.getString("ps_range"));
        String privNames = menuService.havePrivName(info.getString("role_list"));
        session.setAttribute("privNames", privNames);
        try
        {
            String privDeptId="";
            String privDeptLevel ="";
            String privDeptName ="";
            if("".equals(info.getString("oper_dept_id")))
            {
                privDeptId=info.getString("cur_dept_id");
                privDeptLevel=info.getString("cur_dept_level");
                privDeptName=info.getString("cur_dept_name");
            }
            else
            {
                privDeptId=info.getString("oper_dept_id");
                privDeptLevel=info.getString("oper_dept_level");
                privDeptName=info.getString("oper_dept_name");
            }

            session.setAttribute("privDeptName", privDeptName);
            session.setAttribute("privDeptLevel", privDeptLevel);
            session.setAttribute("privDeptId", privDeptId);
        }
        catch(Exception e)
        {
            log.error("取部门层级异常");
        }finally {
            response.sendRedirect("../mainFeet/mainindex.jsp");
        }
    }


}
